/*
 * Copyright (c) 2011, Michael Hengler (developer (at) hengler (dot) de),
 * Nikolaus Moll (developer (at) miblounge (dot) net).
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of the [QTPRJ] nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL [QTPRJ] BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package joodin.impl.widgets.internal;

import joodin.impl.widgets.VaadinComposite;
import my.stuff.vaadin.miglayout.MigLayout;

import org.jowidgets.common.color.IColorConstant;
import org.jowidgets.common.types.Cursor;
import org.jowidgets.common.types.Dimension;
import org.jowidgets.common.widgets.IControlCommon;
import org.jowidgets.common.widgets.controler.IPopupDetectionListener;
import org.jowidgets.common.widgets.descriptor.IWidgetDescriptor;
import org.jowidgets.common.widgets.factory.ICustomWidgetFactory;
import org.jowidgets.common.widgets.factory.IGenericWidgetFactory;
import org.jowidgets.common.widgets.layout.ILayoutDescriptor;
import org.jowidgets.spi.widgets.IPopupMenuSpi;
import org.jowidgets.spi.widgets.IToolBarContainerItemSpi;

import com.vaadin.ui.Panel;

public class ToolBarContainerItemImpl extends ToolBarItemImpl implements IToolBarContainerItemSpi {

	private final VaadinComposite qtComposite;
	private final Panel composite;

	public ToolBarContainerItemImpl(final Panel composite, final IGenericWidgetFactory factory) {
		super(composite);

		this.composite = composite;
		composite.setContent(new MigLayout("", "0[grow]0", "0[grow]0"));
		composite.setCaption("ToolBarContainerItemImpl");

		this.qtComposite = new VaadinComposite(factory, composite);
	}

	@Override
	public <WIDGET_TYPE extends IControlCommon> WIDGET_TYPE add(
		final IWidgetDescriptor<? extends WIDGET_TYPE> descriptor,
		final Object layoutConstraints) {
		final WIDGET_TYPE result = qtComposite.add(descriptor, layoutConstraints); 

		//TODO use new method pack() on controls 
		//if toolbar will be packed, do this for all containers first
//		composite.setWidth(((MigLayout)composite.getContent()).computeSize(composite.getContent()).getWidth(), Sizeable.UNITS_PIXELS);
//		composite.setHeight(((MigLayout)composite.getContent()).computeSize(composite.getContent()).getHeight(), Sizeable.UNITS_PIXELS);
		//getUiReference().setWidth(composite.width());

		return result;
	}

	@Override
	public <WIDGET_TYPE extends IControlCommon> WIDGET_TYPE add(
		final ICustomWidgetFactory<WIDGET_TYPE> factory,
		final Object layoutConstraints) {
		return qtComposite.add(factory, layoutConstraints);
	}

	@Override
	public boolean remove(final IControlCommon control) {
		return qtComposite.remove(control);
	}

	@Override
	public IPopupMenuSpi createPopupMenu() {
		return qtComposite.createPopupMenu();
	}

	@Override
	public void redraw() {
		qtComposite.redraw();
	}

	@Override
	public void setForegroundColor(final IColorConstant colorValue) {
		qtComposite.setForegroundColor(colorValue);
	}

	@Override
	public void setBackgroundColor(final IColorConstant colorValue) {
		qtComposite.setBackgroundColor(colorValue);
	}

	@Override
	public IColorConstant getForegroundColor() {
		return qtComposite.getForegroundColor();
	}

	@Override
	public IColorConstant getBackgroundColor() {
		return qtComposite.getBackgroundColor();
	}

	@Override
	public void setCursor(final Cursor cursor) {
		qtComposite.setCursor(cursor);
	}

	@Override
	public void setVisible(final boolean visible) {
		qtComposite.setVisible(visible);
	}

	@Override
	public boolean isVisible() {
		return qtComposite.isVisible();
	}

	@Override
	public Dimension getSize() {
		return qtComposite.getSize();
	}

	@Override
	public void addPopupDetectionListener(final IPopupDetectionListener listener) {
		qtComposite.addPopupDetectionListener(listener);
	}

	@Override
	public void removePopupDetectionListener(final IPopupDetectionListener listener) {
		qtComposite.removePopupDetectionListener(listener);
	}

	@Override
	public void setLayout(final ILayoutDescriptor layoutDescriptor) {
		qtComposite.setLayout(layoutDescriptor);
	}

	@Override
	public void layoutBegin() {
		qtComposite.layoutBegin();
	}

	@Override
	public void layoutEnd() {
		qtComposite.layoutEnd();
	}

	@Override
	public void removeAll() {
		qtComposite.removeAll();
	}

}
